﻿<template>
  <a-modal
    title="编辑销售退库"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入单据编号" v-decorator="['orderNo']" />
        </a-form-item>
        <a-form-item label="单据大类" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择单据大类" v-decorator="['orderLargeCategory', {rules: [{ required: true, message: '请选择单据大类！' }]}]" @change="onLargeCategorySelect">
            <a-select-option v-for="(item,index) in selectOptions.largeCategories" :key="index" :value="item.orderTypeId">{{ item.orderTypeName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="单据小类" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择单据小类" v-decorator="['orderSubclass', {rules: [{ required: true, message: '请选择单据小类！' }]}]">
            <a-select-option v-for="(item,index) in selectOptions.subclasses" :key="index" :value="item.orderTypeId">{{ item.orderTypeName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="单据总数量" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入单据总数量" style="width: 100%" v-decorator="['orderQuantity']" />
        </a-form-item>
        <a-form-item label="目标仓库Id" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入目标仓库Id" v-decorator="['areaId']" />
        </a-form-item>
        <a-form-item label="目标仓库名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入目标仓库名称" v-decorator="['areaName']" />
        </a-form-item>
        <a-form-item label="退货方" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入退货方" v-decorator="['returnGoods']" />
        </a-form-item>
        <a-form-item label="客户编号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入客户编号" v-decorator="['returnGoodsOrderNo']" />
        </a-form-item>
        <a-form-item label="销售单据" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入销售单据" v-decorator="['saleOrderNo']" />
        </a-form-item>
        <a-form-item label="发货单据" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入发货单据" v-decorator="['dispatchedOrderOrderNo']" />
        </a-form-item>
        <a-form-item label="单据状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择单据状态" v-decorator="['orderStatusEnum', {rules: [{ required: true, message: '请选择单据状态！' }]}]">
            <a-select-option v-for="(item,index) in orderStatusEnumData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    SalesStockReturnOrderEdit,SalesStockReturnOrderLargeCategories,SalesStockReturnOrderSubclass
  } from '@/api/modular/main/SalesStockReturnOrderManage'
  export default {
    data () {
      return {
	    Id: 0,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
		selectOptions:{
		  largeCategories:[],
		  subclasses:[]
		},
        record: {},
        orderStatusEnumData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
			getLargeCategoriesOptions(){
			  if (this.selectOptions.largeCategories.length<=0) {
			    SalesStockReturnOrderLargeCategories().then(d=>{
			      this.selectOptions.largeCategories = d.data || []
			    }).catch(err=>{
			      console.log(err)
			    })
			  }
			},
			getSubclassOptions(id,init=false){
			  SalesStockReturnOrderSubclass(id).then(d=>{
			    this.selectOptions.subclasses = d.data || []
				if (!init){
					this.form.resetFields(['orderSubclass'])
				}
			  }).catch(err=>{
			    this.clearSubclass()
			    console.log(err)
			  })
			},
			onLargeCategorySelect(val){
			  if (val) {
			    this.getSubclassOptions(val)
			  } else {
			    this.clearSubclass()
			  }
			},
			clearSubclass(){
			  this.selectOptions.subclasses = []
			  this.form.resetFields(['orderSubclass'])
			},
      // 初始化方法
      edit (record) {
        this.visible = true;
		this.Id = record.id;
        const orderStatusEnumOption = this.$options
        this.orderStatusEnumData = orderStatusEnumOption.filters['dictData']('order_status')
        this.$nextTick(() => {
          this.form.setFieldsValue(
            {
              id:record.id,
              orderNo: record.orderNo,
              orderLargeCategory: record.orderLargeCategory,
              orderSubclass: record.orderSubclass,
              orderQuantity: record.orderQuantity,
              areaId: record.areaId,
              areaName: record.areaName,
              returnGoods: record.returnGoods,
              returnGoodsOrderNo: record.returnGoodsOrderNo,
              saleOrderNo: record.saleOrderNo,
              dispatchedOrderOrderNo: record.dispatchedOrderOrderNo,
              orderStatusEnum: String(record.orderStatusEnum)
            }
          )
		  this.getLargeCategoriesOptions()
		  this.getSubclassOptions(record.orderLargeCategory,true)
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (values[key] == null) continue
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
                 this.record[key] = values[key]
              } else {
                 this.record[key] = values[key]
              }
            }
            SalesStockReturnOrderEdit(this.record).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          }else{
            this.confirmLoading = false
          } 
        });
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
